package cinescope2014.boundaries;

import java.sql.*;

/**
 *
 * @author pascal
 */
public class JIFPaysUpdate extends javax.swing.JInternalFrame {

    private Connection icn;

    /**
     * Creates new form JIFPaysUpdate
     */
    public JIFPaysUpdate() {
        initComponents();

        setTitle("Modification d'un pays");

        try {
            // --- Connexion
            Class.forName("org.gjt.mm.mysql.Driver");
            String lsDSN = "jdbc:mysql://127.0.0.1:3306/cinescope2014";
            icn = DriverManager.getConnection(lsDSN, "root", "");

            Statement lstSQL = icn.createStatement();
            ResultSet lrs = lstSQL.executeQuery("SELECT nom_pays FROM pays");
            while (lrs.next()) {
                jComboBoxPays.addItem(lrs.getString(1));
            }

            lrs.close();

            jLabelMessage.setText("Okay !!!");

        } catch (ClassNotFoundException | SQLException ex) {
            jLabelMessage.setText(ex.getMessage());
        }

        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBoxPays = new javax.swing.JComboBox();
        jButtonValiderChoix = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldNomPays = new javax.swing.JTextField();
        jLabelMessage = new javax.swing.JLabel();
        jButtonValiderModification = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldMasculin = new javax.swing.JTextField();
        jTextFieldFeminin = new javax.swing.JTextField();
        jTextFieldNeutre = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldId = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jLabel1.setText("Pays à modifier ?");

        jButtonValiderChoix.setText("Valider");
        jButtonValiderChoix.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonValiderChoixActionPerformed(evt);
            }
        });

        jLabel2.setText("Nom du pays");

        jLabelMessage.setText("Message");

        jButtonValiderModification.setText("Valider la modification");
        jButtonValiderModification.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonValiderModificationActionPerformed(evt);
            }
        });

        jLabel3.setText("Masculin");

        jLabel4.setText("Féminin");

        jLabel5.setText("Neutre");

        jLabel6.setText("ID pays");

        jTextFieldId.setBackground(new java.awt.Color(255, 0, 0));
        jTextFieldId.setText("ID PAYS");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                        .addComponent(jComboBoxPays, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 15, Short.MAX_VALUE)
                        .addComponent(jButtonValiderChoix)
                        .addGap(24, 24, 24))
                    .addComponent(jButtonValiderModification, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldMasculin, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldNomPays, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldFeminin, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(67, 67, 67)
                                .addComponent(jTextFieldNeutre, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(63, 63, 63)
                                .addComponent(jTextFieldId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBoxPays, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonValiderChoix))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextFieldId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldNomPays, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldMasculin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldFeminin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldNeutre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonValiderModification)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabelMessage)
                .addGap(6, 6, 6))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonValiderModificationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonValiderModificationActionPerformed
        // TODO add your handling code here:
        try {

            // UPDATE
            String lsSQL = "UPDATE pays SET nom_pays=?, masculin=?, feminin=?, neutre=? WHERE id_pays=?";

            PreparedStatement lpst = icn.prepareStatement(lsSQL);

            lpst.setString(1, jTextFieldNomPays.getText());
            lpst.setString(2, jTextFieldMasculin.getText());
            lpst.setString(3, jTextFieldFeminin.getText());
            lpst.setString(4, jTextFieldNeutre.getText());
            lpst.setString(5, jTextFieldId.getText());

            int liAffectes = lpst.executeUpdate();
            
            System.out.println(liAffectes);

            lpst.close();

            jLabelMessage.setText("Okay pour la modification !!!");

        } catch (SQLException ex) {
            jLabelMessage.setText(ex.getMessage());
        }

    }//GEN-LAST:event_jButtonValiderModificationActionPerformed

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // TODO add your handling code here:
        try {
            icn.close();
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        }
    }//GEN-LAST:event_formInternalFrameClosing

    private void jButtonValiderChoixActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonValiderChoixActionPerformed
        // TODO add your handling code here:
        try {
            PreparedStatement lpstSQL = icn.prepareStatement("SELECT * FROM pays WHERE nom_pays = ?");
            lpstSQL.setString(1, jComboBoxPays.getSelectedItem().toString());
            ResultSet lrs = lpstSQL.executeQuery();
            
            if (lrs.next()) {
                jTextFieldId.setText(lrs.getString(1));
                jTextFieldNomPays.setText(lrs.getString(2));
                jTextFieldMasculin.setText(lrs.getString(3));
                jTextFieldFeminin.setText(lrs.getString(4));
                jTextFieldNeutre.setText(lrs.getString(5));
            }

            lrs.close();
        } catch (SQLException e) {
        }

    }//GEN-LAST:event_jButtonValiderChoixActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonValiderChoix;
    private javax.swing.JButton jButtonValiderModification;
    private javax.swing.JComboBox jComboBoxPays;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabelMessage;
    private javax.swing.JTextField jTextFieldFeminin;
    private javax.swing.JTextField jTextFieldId;
    private javax.swing.JTextField jTextFieldMasculin;
    private javax.swing.JTextField jTextFieldNeutre;
    private javax.swing.JTextField jTextFieldNomPays;
    // End of variables declaration//GEN-END:variables
}
